package com.hjb.service.controller.withdraw;

import com.hjb.service.domain.withdraw.PayCashWithdraw;
import com.hjb.service.domain.withdraw.PayCashWithdrawExample;
import com.hjb.service.gateway.baofoo.domain.request.TransReqBF0040002;
import com.hjb.service.mapper.PayCashWithdrawMapper;
import com.hjb.service.withdraw.FYWithDrawService;
import com.hjb.service.withdraw.WithDrawService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by gy on 2017/7/28.
 * 提现
 */
@RestController
@RequestMapping("/fywithdraw")
public class FYWithDrawController {
	private Logger log = LoggerFactory.getLogger(getClass());

	@Autowired
	private FYWithDrawService fyWithDrawService;
	@Autowired
	private PayCashWithdrawMapper payCashWithdrawMapper;

	/**
	 * 用户提现
	 * @param amount
	 * @param userId
	 * @param orderId
	 * @return
	 */
	@RequestMapping(value = "/withdraw", method = {RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Map<String, Object> withdraw(BigDecimal amount, Integer userId, String orderId) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			PayCashWithdraw payCashWithdraw = new PayCashWithdraw();
			payCashWithdraw.setUserId(userId);
			payCashWithdraw.setAmount(amount);
			payCashWithdraw.setOrderId(orderId);
			resMap = fyWithDrawService.withdraw(payCashWithdraw);
		}catch (Exception e){
			log.info("用户提现请求失败"+e);
		}
		return resMap;
	}

	/**
	 * 提现查询
	 * @param request
	 * @return
	 */
	@RequestMapping("/withdrawQuery")
	@ResponseBody
	public Map<String, Object> withdrawQuery(HttpServletRequest request) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			PayCashWithdrawExample payCashWithdrawExample = new PayCashWithdrawExample();
			payCashWithdrawExample.createCriteria().andStatusEqualTo(2);//等待回调
			List<PayCashWithdraw> payCashWithdrawList = payCashWithdrawMapper.selectByExample(payCashWithdrawExample);
			if (payCashWithdrawList == null || payCashWithdrawList.size()<1){
				log.info("未查到处理中的提现记录");
				return null;
			}
			for (PayCashWithdraw payCashWithdraw:payCashWithdrawList){
				fyWithDrawService.withdrawQuery(payCashWithdraw);
			}
		}catch (Exception e){
			log.info("提现失败");
		}
		return resMap;
	}
}
